
/*
 * Lazy Load - jQuery plugin for lazy loading images
 *
 * Copyright (c) 2007-2009 Mika Tuupola
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Project home:
 *   http://www.appelsiini.net/projects/lazyload
 *
 * Version:  1.5.0
 *
 */
(function($) {

    $.fn.tlazyload = function(options) {
		var timer = null;
        var settings = {
			start	    : null,
			end		    : null,
			startFunction: function() {},
			endFunction: function() {},
            threshold    : 200,
            failurelimit : 0,
            event        : "scroll"
        };

        if(options) {
            $.extend(settings, options);
        }

		settings.container = this;

		var timeOutScroll = function() {
			var counter = 0;
			var checkFunc = function(element, func) {
				if ($.abovethetop(element, settings)) {
						// Nothing
				} else if (!$.belowthefold(element, settings)) {
						func();
				} else {
					if (counter++ > settings.failurelimit) {
						alert('false');
						return false;
					}
				}
				return true;
			};
			
			checkFunc($(settings.start), settings.startFunction);
			checkFunc($(settings.end), settings.endFunction);
			timer = null;
		}

        $(this).bind("scroll", function(event) {
			if (null === timer || undefined === timer)
			{
				timer = setTimeout(timeOutScroll, 500);
			}
        });
		
        return this;
    };

    /* Convenience methods in jQuery namespace.           */
    /* Use as  $.belowthefold(element, {threshold : 100, container : window}) */

    $.belowthefold = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).height() + $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top + $(settings.container).height();
        }
        return fold <= $(element).offset().top - settings.threshold;
    };

    $.rightoffold = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).width() + $(window).scrollLeft();
        } else {
            var fold = $(settings.container).offset().left + $(settings.container).width();
        }
        return fold <= $(element).offset().left - settings.threshold;
    };

    $.abovethetop = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top;
        }
        return fold >= $(element).offset().top + settings.threshold  + $(element).height();
    };

    $.leftofbegin = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).scrollLeft();
        } else {
            var fold = $(settings.container).offset().left;
        }
        return fold >= $(element).offset().left + settings.threshold + $(element).width();
    };
})(jQuery);
